package com.youku
{
   import flash.display.Sprite;
   import com.youku.interfaces.IExternalProxy;
   import com.youku.interfaces.IPlayerProxy;
   import com.youku.events.ExternalInterfaceEvent;
   import com.youku.data.PlayerContext;
   import com.youku.utils.OpenWindow;
   import flash.net.URLRequest;
   
   public class ExternalProxy extends Sprite implements IExternalProxy
   {
      
      public function ExternalProxy() {
         super();
      }
      
      public function addCallBacks() : void {
         try
         {
            if(YoukuExternalInterface.available)
            {
               YoukuExternalInterface.addCallback("nsseek",this.nsseek);
               YoukuExternalInterface.addCallback("setContinuous",this.setContinuous);
               YoukuExternalInterface.addCallback("setSkinColor",this.setSkinColor);
               YoukuExternalInterface.addCallback("pauseVideo",this.pauseVideo);
               YoukuExternalInterface.addCallback("stopVideo",this.stopVideo);
               YoukuExternalInterface.addCallback("setShowMode",this.setShowMode);
               YoukuExternalInterface.addCallback("getNsData",this.getNsData);
               YoukuExternalInterface.addCallback("getSize",this.getSize);
               YoukuExternalInterface.addCallback("PlayerResume",this.PlayerResume);
               YoukuExternalInterface.addCallback("PlayerPause4ad",this.PlayerPause4ad);
               YoukuExternalInterface.addCallback("playVideoByID",this.playVideoByID);
               YoukuExternalInterface.addCallback("setTHX",this.setTHX);
               YoukuExternalInterface.addCallback("setLight",this.setLight);
               YoukuExternalInterface.addCallback("setVolume",this.setVolume);
               YoukuExternalInterface.addCallback("getVolume",this.getVolume);
               YoukuExternalInterface.addCallback("setLoop",this.setLoop);
               YoukuExternalInterface.addCallback("showControlBar",this.showControlBar);
               YoukuExternalInterface.addCallback("recordPosition",this.recordPosition);
               YoukuExternalInterface.addCallback("getPlayerState",this.getPlayerState);
               YoukuExternalInterface.addCallback("getPlaybackQuality",this.getPlaybackQuality);
               YoukuExternalInterface.addCallback("setPlaybackQuality",this.setPlaybackQuality);
               YoukuExternalInterface.addCallback("getAvailableQualityLevels",this.getAvailableQualityLevels);
               YoukuExternalInterface.addCallback("isPause",this.isPause);
               YoukuExternalInterface.addCallback("getJump",this.getJump);
               YoukuExternalInterface.addCallback("setJump",this.setJump);
               YoukuExternalInterface.addCallback("setAdjust",this.setAdjust);
               YoukuExternalInterface.addCallback("getThreed",this.getThreed);
               YoukuExternalInterface.addCallback("setFavorite",this.setFavorite);
               YoukuExternalInterface.addCallback("doInteract",this.doInteract);
               YoukuExternalInterface.addCallback("addInteract",this.addInteract);
               YoukuExternalInterface.addCallback("setLanguage",this.setLanguage);
               YoukuExternalInterface.addCallback("getLanguage",this.getLanguage);
               YoukuExternalInterface.addCallback("zoomOut",this.zoomOut);
            }
         }
         catch(e:Error)
         {
         }
      }
      
      protected function get playerProxy() : IPlayerProxy {
         return GlobalService.getService(GlobalServiceKeys.PLAYER_PROXY) as IPlayerProxy;
      }
      
      public function nsseek(param1:int) : void {
         if(this.playerProxy.apiData.nsseek == 0)
         {
            return;
         }
         dispatchEvent(new ExternalInterfaceEvent(ExternalInterfaceEvent.EI_SEEK,{"time":param1}));
      }
      
      public function setContinuous(param1:Boolean) : void {
         if(this.playerProxy.apiData.setContinuous == 0)
         {
            return;
         }
         dispatchEvent(new ExternalInterfaceEvent(ExternalInterfaceEvent.EI_SET_CONTINUOUS,{"continuous":param1}));
      }
      
      public function setSkinColor(param1:String, param2:String, param3:Number) : void {
      }
      
      public function pauseVideo(param1:Boolean, param2:Boolean = true) : void {
         if(this.playerProxy.apiData.pauseVideo == 0)
         {
            return;
         }
         if(param1)
         {
            dispatchEvent(new ExternalInterfaceEvent(ExternalInterfaceEvent.EI_PAUSE,{"useraction":param2}));
         }
         else
         {
            dispatchEvent(new ExternalInterfaceEvent(ExternalInterfaceEvent.EI_RESUME));
         }
      }
      
      public function stopVideo() : void {
         dispatchEvent(new ExternalInterfaceEvent(ExternalInterfaceEvent.EI_STOP));
      }
      
      public function setShowMode(param1:String) : void {
         dispatchEvent(new ExternalInterfaceEvent(ExternalInterfaceEvent.EI_SET_SHOW_MODE,{"mode":param1}));
      }
      
      public function getNsData() : Object {
         var _loc1_:Object = {};
         _loc1_.time = this.playerProxy.playerData.videoNsTime;
         _loc1_.alltime = this.playerProxy.playerData.videoTotalTime;
         _loc1_.loaded = this.playerProxy.playerData.videoLoadedTime;
         _loc1_.vid = this.playerProxy.playerData.videoIdUncode;
         _loc1_.vidEncoded = this.playerProxy.playerData.videoIdEncode;
         _loc1_.quality = this.playerProxy.playerData.playlistCurrentQuality;
         if(this.playerProxy.playerData.playlistCurrentLangVid != null)
         {
            _loc1_.lang = this.playerProxy.playerData.playlistCurrentLang;
            _loc1_.langVid = this.playerProxy.playerData.playlistCurrentLangVid;
         }
         return _loc1_;
      }
      
      public function getPlayerState() : String {
         var _loc1_:String = this.playerProxy.playerData.videoCurrentFileid;
         var _loc2_:String = this.playerProxy.playerData.playlistCurrentQuality;
         var _loc3_:int = int(this.playerProxy.playerData.videoNsTime);
         var _loc4_:int = int(this.playerProxy.playerData.videoTotalTime);
         var _loc5_:int = int(this.playerProxy.playerData.videoCurrentNsTime);
         var _loc6_:int = int(this.playerProxy.playerData.videoCurrentTotalTime);
         var _loc7_:int = int(this.playerProxy.playerData.videoLoadedTime);
         var _loc8_:int = int(this.playerProxy.playerData.videoCurrentLoadedTime);
         var _loc9_:Number = this.playerProxy.playerData.videoCurrentNsTime;
         var _loc10_:String = new Array(_loc1_,_loc2_,_loc3_,_loc4_,_loc5_,_loc6_,_loc7_,_loc8_,_loc9_).join("|");
         return _loc10_;
      }
      
      public function getSize() : Object {
         var _loc1_:Object = {};
         _loc1_.width = PlayerConfig.normalWidth;
         _loc1_.height = PlayerConfig.normalHeight;
         _loc1_.videoWidth = this.playerProxy.playerData.videoCurrentWidth;
         _loc1_.videoHeight = this.playerProxy.playerData.videoCurrentHeight;
         _loc1_.cheight = PlayerConfig.controlBarHeight;
         return _loc1_;
      }
      
      public function showControlBar(param1:Boolean) : void {
         if(this.playerProxy.apiData.showControlBar == 0)
         {
            return;
         }
         dispatchEvent(new ExternalInterfaceEvent(ExternalInterfaceEvent.EI_SHOW_CONTROLBAR,{"show":param1}));
      }
      
      public function playVideoByID(param1:String, param2:String = "") : void {
         if(this.playerProxy.apiData.playVideoByID == 0)
         {
            return;
         }
         dispatchEvent(new ExternalInterfaceEvent(ExternalInterfaceEvent.EI_PLAY_NEW_VIDEO,
            {
               "videoid":param1,
               "quality":param2
            }));
      }
      
      public function PlayerResume() : void {
      }
      
      public function PlayerPause4ad() : void {
      }
      
      public function setTHX(param1:String) : void {
         dispatchEvent(new ExternalInterfaceEvent(ExternalInterfaceEvent.EI_SET_THX,{"state":param1}));
      }
      
      public function setLight(param1:String) : void {
         dispatchEvent(new ExternalInterfaceEvent(ExternalInterfaceEvent.EI_SET_LIGHT,{"state":param1}));
      }
      
      public function recordPosition() : void {
         dispatchEvent(new ExternalInterfaceEvent(ExternalInterfaceEvent.EI_RECORD_POSITION));
      }
      
      public function setVolume(param1:Number) : void {
         if(this.playerProxy.apiData.setVolume == 0)
         {
            return;
         }
         dispatchEvent(new ExternalInterfaceEvent(ExternalInterfaceEvent.EI_SET_VOLUME,{"volume":param1}));
      }
      
      public function getVolume() : Number {
         return PlayerContext.volume;
      }
      
      public function setLoop(param1:Boolean) : void {
         if(this.playerProxy.apiData.setLoop == 0)
         {
            return;
         }
         dispatchEvent(new ExternalInterfaceEvent(ExternalInterfaceEvent.EI_SET_LOOP,{"isLoop":param1}));
      }
      
      public function getPlaybackQuality() : String {
         return this.playerProxy.playerData.playlistCurrentQuality;
      }
      
      public function setPlaybackQuality(param1:String) : void {
         if(this.playerProxy.apiData.setPlaybackQuality == 0)
         {
            return;
         }
         if(!(param1 == "auto") && !(param1 == "low") && !(param1 == "mid") && !(param1 == "high"))
         {
            return;
         }
         dispatchEvent(new ExternalInterfaceEvent(ExternalInterfaceEvent.EI_SET_QUALITY,{"quality":param1}));
      }
      
      public function getAvailableQualityLevels() : Array {
         return this.playerProxy.playerData.playlistTypeArr;
      }
      
      public function getJump() : Boolean {
         return this.playerProxy.playerData.isJump;
      }
      
      public function setJump(param1:Boolean) : void {
         if(this.playerProxy.apiData.setJump == 0)
         {
            return;
         }
         dispatchEvent(new ExternalInterfaceEvent(ExternalInterfaceEvent.EI_SET_JUMP,{"jump":param1}));
      }
      
      public function setAdjust(param1:Number = 0, param2:Number = 0, param3:Number = 0) : void {
         var _loc4_:Object = {};
         _loc4_.bright = param1;
         _loc4_.contrast = param2;
         _loc4_.saturation = param3;
         dispatchEvent(new ExternalInterfaceEvent(ExternalInterfaceEvent.EI_SET_ADJUST_PARAMETERS,_loc4_));
      }
      
      public function setLanguage(param1:String) : void {
         dispatchEvent(new ExternalInterfaceEvent(ExternalInterfaceEvent.EI_SET_LANGUAGE,{"lang":param1}));
      }
      
      public function getLanguage() : String {
         return this.playerProxy.playerData.playlistCurrentLang;
      }
      
      public function zoomOut() : void {
         dispatchEvent(new ExternalInterfaceEvent(ExternalInterfaceEvent.EI_ZOOM_OUT));
      }
      
      public function isPause() : Boolean {
         return this.playerProxy.playerData.videoIsPause;
      }
      
      public function hasWatermark() : Boolean {
         return this.playerProxy.playerData.watermark;
      }
      
      public function get isIKuInstall() : Boolean {
         var addflag:String = "";
         try
         {
            addflag = YoukuExternalInterface.call("getP2PState",Math.random());
         }
         catch(e:Error)
         {
            addflag = "";
         }
         if(!(addflag == null) && !(addflag == "") && !(addflag == "null") && !(int(addflag) == 0))
         {
            addflag = "y";
         }
         else
         {
            addflag = "n";
         }
         if(addflag == "y")
         {
            return true;
         }
         return false;
      }
      
      public function get IkuFlag() : String {
         var addflag:String = "";
         try
         {
            addflag = YoukuExternalInterface.call("getP2PState",Math.random());
         }
         catch(e:Error)
         {
            addflag = "";
         }
         if(!(addflag == null) && !(addflag == "") && !(addflag == "null") && !(int(addflag) == 0))
         {
            addflag = addflag;
         }
         else
         {
            addflag = "";
         }
         return addflag;
      }
      
      public function getIKuState(param1:String, param2:String = null) : String {
         var action:String = param1;
         var quality:String = param2;
         if(quality == null)
         {
            quality = this.playerProxy.playerData.playlistCurrentQuality;
         }
         var state:String = "";
         try
         {
            state = YoukuExternalInterface.call("getP2PState",Math.random(),action,quality);
         }
         catch(e:Error)
         {
            state = "";
         }
         if(!(state == null) && !(state == "") && !(state == "null") && !(int(state) == 0))
         {
            state = state;
         }
         else
         {
            state = "";
         }
         return state;
      }
      
      public function get IKuId() : String {
         var ikuid:String = "";
         try
         {
            ikuid = YoukuExternalInterface.call("getIkuId",Math.random());
         }
         catch(e:Error)
         {
            ikuid = "";
         }
         if(!(ikuid == null) && !(ikuid == ""))
         {
            return ikuid;
         }
         return "";
      }
      
      public function playerStateChange(param1:String) : void {
         var type:String = param1;
         var state:String = this.getPlayerState();
         try
         {
            YoukuExternalInterface.call("playerStateChange",type,state);
         }
         catch(e:Error)
         {
         }
      }
      
      public function getReferrer() : String {
         var referrer:String = "";
         try
         {
            referrer = YoukuExternalInterface.call("func_referrer");
         }
         catch(e:Error)
         {
            referrer = "";
         }
         if(!referrer)
         {
            referrer = "";
         }
         return referrer;
      }
      
      public function getHref() : String {
         var href:String = "";
         try
         {
            href = String(YoukuExternalInterface.call("window.location.href.toString"));
         }
         catch(e:Error)
         {
            href = "";
         }
         return href;
      }
      
      public function getThreed() : Object {
         var _loc1_:Object = new Object();
         _loc1_.threed = false;
         _loc1_.width = PlayerConfig.normalWidth;
         _loc1_.height = PlayerConfig.normalHeight - PlayerConfig.controlBarHeight;
         _loc1_.fullscreen = false;
         _loc1_.pause = false;
         if(this.playerProxy.playerData.threedInfo)
         {
            if(this.playerProxy.playerData.threedInfo.isTVMode == true)
            {
               _loc1_.threed = true;
            }
            if(!(this.playerProxy.playerData.threedInfo.threedWidth == 0) && !(this.playerProxy.playerData.threedInfo.threedHeight == 0))
            {
               _loc1_.width = this.playerProxy.playerData.threedInfo.threedWidth;
               _loc1_.height = this.playerProxy.playerData.threedInfo.threedHeight;
            }
            _loc1_.fullscreen = this.playerProxy.playerData.threedInfo.fullscreen;
            _loc1_.pause = this.playerProxy.playerData.threedInfo.pause;
         }
         _loc1_.mode = "lr";
         return _loc1_;
      }
      
      public function setFavorite(param1:Boolean) : void {
         dispatchEvent(new ExternalInterfaceEvent(ExternalInterfaceEvent.EI_SET_FAVORITE,{"state":param1}));
      }
      
      public function doInteract(param1:Object) : void {
         dispatchEvent(new ExternalInterfaceEvent(ExternalInterfaceEvent.EI_DO_INTERACT,param1));
      }
      
      public function addInteract(param1:Object) : void {
         dispatchEvent(new ExternalInterfaceEvent(ExternalInterfaceEvent.EI_ADD_INTERACT,param1));
      }
      
      public function onPlayerSet(param1:Object) : void {
         var para:Object = param1;
         try
         {
            YoukuExternalInterface.call("onPlayerSet",para);
         }
         catch(e:Error)
         {
         }
      }
      
      public function onPlayerStart(param1:Object) : void {
         var v:Object = param1;
         try
         {
            YoukuExternalInterface.call("onPlayerStart",v);
         }
         catch(e:Error)
         {
         }
      }
      
      public function onPlayerComplete(param1:Object) : void {
         var v:Object = param1;
         try
         {
            YoukuExternalInterface.call("onPlayerComplete",v);
         }
         catch(e:Error)
         {
         }
      }
      
      public function onPlayerError(param1:Object) : void {
         var v:Object = param1;
         try
         {
            YoukuExternalInterface.call("onPlayerError",v);
         }
         catch(e:Error)
         {
         }
      }
      
      public function onTrialEnd() : void {
         try
         {
            YoukuExternalInterface.call("onTrialEnd");
         }
         catch(e:Error)
         {
         }
      }
      
      public function onTrialLinkClick() : void {
         var _loc1_:* = "http://pay.youku.com/buy/redirect.html";
         _loc1_ = _loc1_ + ("?psid=" + String(PlayerConfig.playListData.show.showID));
         _loc1_ = _loc1_ + "&pstype=1";
         OpenWindow.open(new URLRequest(_loc1_),"_blank");
      }
      
      public function onPlayerPreAdInit(param1:Object, param2:Object, param3:Object, param4:Object, param5:Object) : void {
      }
      
      public function onPlayerLastAdInit(param1:Object, param2:Object, param3:Object, param4:Object, param5:Object) : void {
      }
      
      public function addScriptContent(param1:Object) : void {
      }
      
      public function PlayerPlayPre(param1:Object) : void {
         var v:Object = param1;
         try
         {
            YoukuExternalInterface.call("PlayerPlayPre",v);
         }
         catch(e:Error)
         {
         }
      }
      
      public function PlayerPlayNext(param1:Object) : void {
         var v:Object = param1;
         try
         {
            YoukuExternalInterface.call("PlayerPlayNext",v);
         }
         catch(e:Error)
         {
         }
      }
      
      public function THXOff() : void {
         try
         {
            YoukuExternalInterface.call("THX.off");
         }
         catch(e:Error)
         {
         }
      }
      
      public function THXOn() : void {
         try
         {
            YoukuExternalInterface.call("THX.on");
         }
         catch(e:Error)
         {
         }
      }
      
      public function THXTurn() : void {
         try
         {
            YoukuExternalInterface.call("THX.turn");
         }
         catch(e:Error)
         {
         }
      }
      
      public function LightOff() : void {
         try
         {
            YoukuExternalInterface.call("Light.off");
         }
         catch(e:Error)
         {
         }
      }
      
      public function LightOn() : void {
         try
         {
            YoukuExternalInterface.call("Light.on");
         }
         catch(e:Error)
         {
         }
      }
      
      public function LightTurn() : void {
         try
         {
            YoukuExternalInterface.call("Light.turn");
         }
         catch(e:Error)
         {
         }
      }
      
      public function openMiniWin() : void {
         try
         {
            YoukuExternalInterface.call("openMiniWin");
         }
         catch(e:Error)
         {
         }
      }
      
      public function commentFocus() : void {
         try
         {
            YoukuExternalInterface.call("commentFocus");
         }
         catch(e:Error)
         {
         }
      }
      
      public function playerVideoFav(param1:String) : void {
         var vid:String = param1;
         try
         {
            YoukuExternalInterface.call("playerVideoFav",vid);
         }
         catch(e:Error)
         {
         }
      }
      
      public function htmlCloseCall(param1:String) : void {
         var jsStr:String = null;
         var jsStr2:String = null;
         var url:String = param1;
         try
         {
            jsStr = "eval(\"var youku_playerp2p_close=false;window.onbeforeunload=function(){if(!youku_playerp2p_close)return;var img = new Image();img.src = \'" + url + "\';setTimeout(function(){},1000);}\")";
            YoukuExternalInterface.call(jsStr);
            jsStr2 = "eval(\"youku_playerp2p_close=true\")";
            YoukuExternalInterface.call(jsStr2);
         }
         catch(e:Error)
         {
         }
      }
      
      public function onPlayerStageVideo(param1:Boolean) : void {
         var flag:Boolean = param1;
         try
         {
            YoukuExternalInterface.call("onPlayerStageVideo",flag);
         }
         catch(e:Error)
         {
         }
      }
   }
}
